package com.blue.Impl.system;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blue.common.constant.CommonConstant;
import com.blue.common.utils.ThreadLocalUtils;
import com.blue.po.author.UserPO;
import com.blue.po.workflow.TaskInstancePO;
import com.blue.service.author.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blue.mapper.system.MessageMapper;
import com.blue.po.system.MessagePO;
import com.blue.service.system.IMessageService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessagePO> implements IMessageService {
    @Autowired
    private IUserService userService;

    @Override
    public Map<String, Long> unReadCount() {
        long sysMessageCount = this.count(new LambdaQueryWrapper<MessagePO>()
                .eq(MessagePO::getMsgStatus, CommonConstant.UNREAD)
                .eq(MessagePO::getMsgType, CommonConstant.SYS_MESSAGE)
                .eq(MessagePO::getRecipient, ThreadLocalUtils.getLoginUserInfo().getUserId()));
        long messageCount = this.count(new LambdaQueryWrapper<MessagePO>()
                .eq(MessagePO::getMsgStatus, CommonConstant.UNREAD)
                .eq(MessagePO::getMsgType, CommonConstant.MESSAGE)
                .eq(MessagePO::getRecipient, ThreadLocalUtils.getLoginUserInfo().getUserId()));
        long todoCount = this.count(new LambdaQueryWrapper<MessagePO>()
                .eq(MessagePO::getMsgStatus, CommonConstant.UNREAD)
                .eq(MessagePO::getMsgType, CommonConstant.TODO)
                .eq(MessagePO::getRecipient, ThreadLocalUtils.getLoginUserInfo().getUserId()));
        long sum = sysMessageCount + messageCount + todoCount;
        Map<String, Long> map = new HashMap<>();
        map.put("sysMessageCount", sysMessageCount);
        map.put("messageCount", messageCount);
        map.put("todoCount", todoCount);
        map.put("sum", sum);
        return map;
    }

    @Override
    public void sendTodoMessage(String instanceId, List<TaskInstancePO> taskInstancePOS, String definitionNm) {
        List<MessagePO> messagePOS = new ArrayList<>();
        taskInstancePOS.forEach(item -> {
            MessagePO messagePO = new MessagePO();
            messagePO.setSender("system");
            messagePO.setRecipient(item.getAssigneeId());
            messagePO.setMsgStatus(CommonConstant.UNREAD);
            messagePO.setMsgType(CommonConstant.TODO);
            messagePO.setRoutePath("/workflow/workflowDetail");
            Map<String, String> map = new HashMap<>();
            map.put("instanceId", instanceId);
            map.put("taskId", item.getId());
            map.put("definitionNm", definitionNm);
            messagePO.setRouteParam(JSON.toJSONString(map));
            messagePO.setTheme("您有一条新的" + definitionNm + "事务待处理!");
            messagePO.setContent("点击右下角进入审批详情及时处理！");
            messagePOS.add(messagePO);
        });
        this.saveBatch(messagePOS);
    }

    @Override
    public void sendSystemMessage(String recipient, String instanceId, String status, String definitionNm) {
        MessagePO messagePO = new MessagePO();
        messagePO.setSender("system");
        messagePO.setRecipient(recipient);
        messagePO.setMsgStatus(CommonConstant.UNREAD);
        messagePO.setMsgType(CommonConstant.SYS_MESSAGE);
        messagePO.setRoutePath("/workflow/workflowDetail");
        Map<String, String> map = new HashMap<>();
        map.put("instanceId", instanceId);
        map.put("definitionNm", definitionNm);
        messagePO.setRouteParam(JSON.toJSONString(map));
        messagePO.setTheme("你提交的" + definitionNm + "审批流程已完成！");
        if (CommonConstant.COMPLETED.equals(status)) {
            messagePO.setContent("好消息！你提交的" + definitionNm + "流程审批通过啦！");
        } else if (CommonConstant.REJECTED.equals(status)) {
            messagePO.setContent("很遗憾！你提交的" + definitionNm + "流程审批被拒绝了！");
        } else if (CommonConstant.BACK.equals(status)) {
            messagePO.setContent("很抱歉！你提交的" + definitionNm + "流程审批被退回了！");
        }
        this.save(messagePO);
    }

    @Override
    public void sendCcToMessage(List<String> ccToIds, String instanceId, String definitionNm) {
        List<MessagePO> messagePOS = new ArrayList<>();
        ccToIds.forEach(item -> {
            MessagePO messagePO = new MessagePO();
            String userId = ThreadLocalUtils.getLoginUserInfo().getUserId();
            messagePO.setSender(userId);
            messagePO.setRecipient(item);
            messagePO.setMsgStatus(CommonConstant.UNREAD);
            messagePO.setMsgType(CommonConstant.MESSAGE);
            messagePO.setRoutePath("/workflow/workflowDetail");
            Map<String, String> map = new HashMap<>();
            map.put("instanceId", instanceId);
            map.put("definitionNm", definitionNm);
            messagePO.setRouteParam(JSON.toJSONString(map));
            UserPO userPO = userService.getOne(new LambdaQueryWrapper<UserPO>()
                    .select(UserPO::getUsername)
                    .eq(UserPO::getUserId, userId));
            messagePO.setTheme("您有一条新的" + definitionNm + "事务抄送消息待查看!");
            messagePO.setContent(userPO.getUsername() + "给你发送了一条" + definitionNm + "事务抄送消息，点击右下角进入审批详情查看！");
            messagePOS.add(messagePO);
        });
        this.saveBatch(messagePOS);
    }
}
